#include "Expense.h"
using namespace std;

//@author A0088381W
// constructor for Expense class, take in time and store in time_t type
Expense::Expense( string name, double amount, time_t timeOfRecord, e_method method, e_category category, string remark): Entry ( name, amount) {
	_timeOfRecord = timeOfRecord;
	_method = method;
	_category = category;
	_remark = remark;
}

Expense::Expense( string name, double amount, time_t timeOfRecord, time_t timeOfInput, e_method method, e_category category, string remark): Entry ( name, amount, timeOfInput) {
	_timeOfRecord = timeOfRecord;
	_method = method;
	_category = category;
	_remark = remark;
}

//@author A0088381W
int Expense::encodeCategory(e_category category) {
	int result;
	switch (category) {
		case Food:
			result = 1;
			break;
		case Transportation:
			result = 2;
			break;
		case Entertainment:
			result = 3;
			break;
		case Others:
			result = 4;
			break;
	}

	return result;
}

//@author A0088381W
e_category Expense::decodeCategory(int i) {
	e_category result;
	switch (i) {
		case 1:
			result = Food;
			break;
		case 2:
			result = Transportation;
			break;
		case 3:
			result = Entertainment;
			break;
		case 4:
			result = Others;
			break;
	}

	return result;
}

//@author U095387X
//Deserialize conturctor
Expense::Expense( string name, string objectString, string remark){

	int met, cate;

	_name = name;
	_remark = remark;

	istringstream instr(objectString);


	instr >> _amount >> _timeOfRecord >> _timeOfInput >> met >> cate;
	_method = decodeMethod(met);
	_category = decodeCategory(cate);

	
}

//@author A0088381W
int Expense::encodeMethod (e_method method) {
	int result;
	switch (method) {
		case Cash:
			result = 1;
			break;
		case CreditCard:
			result = 2;
			break;
		case DebitCard:
			result = 3;
			break;
	}

	return result;
}

//@author A0088381W
e_method Expense::decodeMethod(int i) {
	e_method result;
	switch (i) {
		case 1:
			result = Cash;
			break;
		case 2:
			result = CreditCard;
			break;
		case 3:
			result = DebitCard;
			break;
	}

	return result;
}


//@author U095387X
string Expense::serialize(){
	ostringstream outstr;
	int category, method;
	string test;

	outstr << _name << endl;
	category =	encodeCategory(_category);
	method = encodeMethod(_method);
	outstr << _amount << " " << _timeOfRecord << " " << _timeOfInput << " " << method << " " << category << endl;
	outstr << getRemark();
	test=outstr.str();
	return outstr.str();
}

//@author A0088381W
time_t Expense::getTimeOfRecord() {
	return _timeOfRecord;
}

//@author A0088381W
e_category Expense::getCategory() {
	return _category;
}

//@author A0088381W
e_method Expense::getMethod() {
	return _method;
}

//@author A0088381W
string Expense::getRemark() {
	return _remark;
}

//@author U095387X
bool Expense::operator<(Expense &b){
	if(_timeOfRecord < b.getTimeOfRecord())
		return true;
	else
		return false;

}